package WizardTD.element;

import WizardTD.App;
import WizardTD.Coordinate;
import WizardTD.DataConstruct;
import lombok.Data;
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;

import java.util.List;

/**
 *
 */
@Data
public class Monster extends Element {
    private String type;
    private float hp;
    private Integer speed;
    private Double armour;
    private PImage pImage;
    private Integer mana_gained_on_kill;

    private boolean isSubMana;
    private Integer quantity;
    private int monsterIndex = 0;
    private DataConstruct dataConstruct;
    private PVector monsterPosition;
    private PVector currentPos;
    private PVector nextPos;
    private PVector step;

    private BloodProcessBar bloodProcessBar;
    private List<Coordinate> shortestPath;

    private Integer frameCount = 0;

    public Monster(float coordinateX, float coordinateY, PImage pImage, Monster monster, DataConstruct dataConstruct) {
        super(coordinateX, coordinateY);
        this.pImage = pImage;
        this.type = monster.type;
        this.hp = monster.hp;
        this.speed = monster.speed;
        this.armour = monster.armour;
        this.mana_gained_on_kill = monster.mana_gained_on_kill;
        this.dataConstruct = dataConstruct;
        monsterPosition = new PVector(coordinateX, coordinateY);
        this.bloodProcessBar = new BloodProcessBar(monster, coordinateX, coordinateY);
        this.shortestPath = dataConstruct.shortestPath;
    }

    public Monster(float coordinateX, float coordinateY, PImage pImage, Monster monster) {
        super(coordinateX, coordinateY);
        this.pImage = pImage;
        this.type = monster.type;
        this.hp = monster.hp;
        this.speed = monster.speed;
        this.armour = monster.armour;
        this.mana_gained_on_kill = monster.mana_gained_on_kill;
        monsterPosition = new PVector(coordinateX, coordinateY);
    }

    public Monster() {
    }

    /**
     * 画怪物
     *
     * @param pApplet pApplet
     */
    @Override
    public void draw(PApplet pApplet) {
        updateCoordinate(pApplet);
        this.bloodProcessBar.draw(pApplet);
        pApplet.image(pImage, this.getCoordinateX(), this.getCoordinateY(), App.CELLSIZE, App.CELLSIZE);
    }

    public void updateCoordinate(PApplet pApplet) {
        this.drawMonster(pApplet);
    }

    public boolean isArriveLast() {
        return this.getCoordinateX() == shortestPath.get(shortestPath.size() - 1).col * App.CELLSIZE
                && this.getCoordinateY() == (shortestPath.get(shortestPath.size() - 1).row + 1) * App.CELLSIZE;
    }

    /**
     * @param pApplet PApplet
     *                生成怪兽
     */
    public void drawMonster(PApplet pApplet) {
        if (this.monsterIndex < shortestPath.size() - 1) {
            Coordinate currentCoord = shortestPath.get(this.monsterIndex);
            Coordinate nextCoord = shortestPath.get(this.monsterIndex + 1);
            this.currentPos = new PVector(currentCoord.col * App.CELLSIZE, currentCoord.row * App.CELLSIZE + App.CELLSIZE);
            this.nextPos = new PVector(nextCoord.col * App.CELLSIZE, nextCoord.row * App.CELLSIZE + App.CELLSIZE);
            this.step = PVector.sub(nextPos, currentPos).normalize().mult(this.speed);
            monsterPosition.add(step);
            this.setCoordinateX(this.getCoordinateX() + step.x);
            this.setCoordinateY(this.getCoordinateY() + step.y);
            this.bloodProcessBar.setX(this.getCoordinateX() + step.x);
            this.bloodProcessBar.setY(this.getCoordinateY() + step.y);
            if (PVector.dist(monsterPosition, nextPos) < 1.0) {
                monsterIndex++;
            }
        }
        frameCount++;
    }

}
